﻿using DLog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DLogQueryWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DLog.ILogger logger;
        static readonly int pageSize = 20;

        public MainWindow()
        {
            InitializeComponent();
            logger = DLog.LogManager.GetCurrentClassLogger();

            Dictionary<int, String> StringDict = new Dictionary<int, String>() { 
               {(int)EnumLogLevel.Trace, "Trace"}, 
               {(int)EnumLogLevel.Debug, "Debug"}, 
               {(int)EnumLogLevel.Info, "Info"}, 
               {(int)EnumLogLevel.Warn, "Warn"}, 
               {(int)EnumLogLevel.Error, "Error"}, 
               {(int)EnumLogLevel.Fatal, "Fatal"},
            };
            cbLogLevel.ItemsSource = StringDict;
            cbLogLevel.DisplayMemberPath = "Value";
            cbLogLevel.SelectedValuePath = "Key";
            DataGridBind(null, null, null, true);
        }

        public string LogLevelToStr(int i)
        {
            switch ((EnumLogLevel)i)
            {
                case EnumLogLevel.Trace:
                    return "Trace";
                case EnumLogLevel.Debug:
                    return "Debug";
                case EnumLogLevel.Warn:
                    return "Warn";
                case EnumLogLevel.Error:
                    return "Error";
                case EnumLogLevel.Fatal:
                    return "Fatal";
                default:
                    return "Info";
            }
        }

        private void DataGridBind(int? logLevel, DateTime? createStartTime, DateTime? createEndTime, bool isInit)
        {
            int totalCount = 0;
            if (isInit)
            {
                tbkCurrentIndex.Text = "1";
            }
            if (logLevel != null && createStartTime != null && createEndTime != null)
            {
                dgLog.DataContext = logger.GetLogs((int)logLevel, (DateTime)createStartTime, (DateTime)createEndTime, Convert.ToInt32(tbkCurrentIndex.Text), pageSize, out totalCount);
                tbkTotalPage.Text = (totalCount / pageSize + 1).ToString();
            }
            else if (logLevel != null && (createStartTime == null || createEndTime == null))
            {
                dgLog.DataContext = logger.GetLogs((int)logLevel, Convert.ToInt32(tbkCurrentIndex.Text), pageSize, out totalCount);
                tbkTotalPage.Text = (totalCount / pageSize + 1).ToString();
            }
            else if (logLevel == null && createStartTime != null && createEndTime != null)
            {
                dgLog.DataContext = logger.GetLogs((DateTime)createStartTime, (DateTime)createEndTime, Convert.ToInt32(tbkCurrentIndex.Text), pageSize, out totalCount);
                tbkTotalPage.Text = (totalCount / pageSize + 1).ToString();
            }
            else
            {
                dgLog.DataContext = logger.GetLogs(Convert.ToInt32(tbkCurrentIndex.Text), pageSize, out totalCount);
                tbkTotalPage.Text = (totalCount / pageSize + 1).ToString();
            }
            if (Convert.ToInt32(tbkCurrentIndex.Text) == 1)
            {
                btnUp.IsEnabled = false;
            }
            else
            {
                btnUp.IsEnabled = true;
            }
            if (Convert.ToInt32(tbkCurrentIndex.Text) == Convert.ToInt32(tbkTotalPage.Text))
            {
                btnNext.IsEnabled = false;
            }
            else
            {
                btnNext.IsEnabled = true;
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            DataGridBind(cbLogLevel.SelectedValue.AsInt(), dpCreateStartDate.Text.AsDateTime(), dpCreateEndDate.Text.AsDateTime(), true);
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(tbxPageNum.Text.AsInt() ?? -1) <= Convert.ToInt32(tbkTotalPage.Text) && Convert.ToInt32(tbxPageNum.Text.AsInt() ?? -1) > 0)
            {
                tbkCurrentIndex.Text = tbxPageNum.Text;
                DataGridBind(cbLogLevel.SelectedValue.AsInt(), dpCreateStartDate.Text.AsDateTime(), dpCreateEndDate.Text.AsDateTime(), false);
            }
            else
            {
                MessageBox.Show("超出页数范围！");
            }
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(tbkCurrentIndex.Text) - 1 >= 1)
            {
                tbkCurrentIndex.Text = (Convert.ToInt32(tbkCurrentIndex.Text) - 1).ToString();
                DataGridBind(cbLogLevel.SelectedValue.AsInt(), dpCreateStartDate.Text.AsDateTime(), dpCreateEndDate.Text.AsDateTime(), false);
            }
            else
            {
                MessageBox.Show("超出页数范围！");
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(tbkCurrentIndex.Text) + 1 <= Convert.ToInt32(tbkTotalPage.Text))
            {
                tbkCurrentIndex.Text = (Convert.ToInt32(tbkCurrentIndex.Text) + 1).ToString();
                DataGridBind(cbLogLevel.SelectedValue.AsInt(), dpCreateStartDate.Text.AsDateTime(), dpCreateEndDate.Text.AsDateTime(), false);
            }
            else
            {
                MessageBox.Show("超出页数范围！");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbLogLevel.Items.Insert(0, "select log level");
        }
    }
}
